package com.bckj.fastboot.security.model;

import cn.hutool.core.map.MapUtil;
import com.bckj.fastboot.security.constant.SecurityConstants;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;

import java.io.Serial;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class AuthUser extends User implements OAuth2AuthenticatedPrincipal {

    @Serial
    private final static long serialVersionUID = 9527L;

    /**
     * 扩展属性，方便存放oauth 上下文相关信息
     */
    private final Map<String, Object> attributes = new HashMap<>();

    @Getter
    private final Long id;
    @Getter
    private final Long deptId;
    @Getter
    private final String phone;

    public AuthUser(Long id, Long deptId, String username, String password, String phone, boolean enabled,
                    boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked,
                    Collection<? extends GrantedAuthority> authorities) {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        this.id = id;
        this.deptId = deptId;
        this.phone = phone;
    }

    @Override
    public String getName() {
        return getUsername();
    }

    @Override
    public Map<String, Object> getAttributes() {
        return attributes;
    }

    public String getClientId() {
        return MapUtil.getStr(getAttributes(), SecurityConstants.CLIENT_ID);
    }
}
